package com.lovo.service.impl;

import com.lovo.dao.UserDAO;
import com.lovo.dao.impl.UserDAOImpl;
import com.lovo.pojo.UesrDO;
import com.lovo.service.UserService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    private UserDAO userDAO =new UserDAOImpl();


    @Override
    public UesrDO getByUserNameAndPWd(String username, String pwd) {
        return userDAO.getByUserNameAndPWd(username,pwd);
    }

    @Override
    public UesrDO getById(int id) {
       return userDAO.getById(id);
    }


    @Override
    public void saverMoney(int id, int blance) {
        UesrDO uesrDO =userDAO.getById(id);
        uesrDO.setBalance(uesrDO.getBalance()+blance);
        userDAO.update(uesrDO);
    }

    @Override
    public void getMoney(int id, int blance) throws Exception {
        UesrDO uesrDO=userDAO.getById(id);
        if (uesrDO.getBalance()<blance){
            throw new Exception("余额不足");
        }
        uesrDO.setBalance(uesrDO.getBalance()-blance);
        userDAO.update(uesrDO);
    }

    @Override
    public void upPwd(int id,String password,String newPassword,String conPassword) throws Exception {
        UesrDO uesrDO =userDAO.getById(id);
        if (uesrDO.getPassword().equals(password)){
            if (newPassword.equals(conPassword)){
                uesrDO.setPassword(conPassword);
                userDAO.update(uesrDO);
            }else {
                throw new Exception("新密码与确认密码不同");
            }
        }else {
            throw new Exception("你输入的密码与旧密码不同");
        }
    }

    @Override
    public void transfer(int id, String otherUser, int money) throws Exception {
        UesrDO uesrDO =userDAO.getById(id);
        UesrDO otherUesrDO=userDAO.getByName(otherUser);
        if (uesrDO.getBalance()<money){
            throw new Exception("余额不足");
        }
        if (uesrDO.getAccount().equals(otherUser)){
            throw new Exception("转账不能是自己");
        }
        if(otherUser==null){
            throw new Exception("此人不存在");
        }
        uesrDO.setBalance(uesrDO.getBalance()-money);
        otherUesrDO.setBalance(otherUesrDO.getBalance()+money);
        userDAO.update(uesrDO);
        userDAO.update(otherUesrDO);
    }

    //查询



}
